<?php
/**
 * Description:
 * Author: hezhizheng
 * Date: 2018/11/14
 * Time: 14:32
 * Created by Created by Panxsoft.
 */

namespace App\Models;

use App\Models\Products\Product;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * Class StockStatus
 *
 * @property string name
 * @method static self filterProducts(?array $product_ids)
 *
 * @package App\Models
 * @mixin Builder
 */
class StockStatus extends Model
{
    protected $guarded = [];

    protected $table = 'stock_status';

    protected $primaryKey = 'stock_status_id';

    /**
     * @used-by filterProducts
     *
     * @param Builder $query
     * @param array $product_ids
     */
    public function scopeFilterProducts(Builder $query, ?array $product_ids): void
    {
        if (is_array($product_ids)) {
            $self_ids = (new Product)
                ->whereKey($product_ids)
                ->distinct()
                ->pluck($this->primaryKey);

            $query->whereIn($this->getQualifiedKeyName(), $self_ids);
        }
    }
}